﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ALDIOrganizer.Properties;

namespace ALDIOrganizer
{
    internal class PopupMenuLayout
    {
        internal PopupMenuEventHandlers _handlers;
        /// <summary>
        /// Is the SheetView for which the menu will be used ReadOnly?
        /// </summary>
        /// <remarks>
        /// If it is, the option of adding/deleting Shifts should be disabled (greyed out)
        /// </remarks>
        bool _IsReadOnly;

        internal void Bind(Sheet pRefill, SheetView pDataGrid, Cell pCell, int pRow)
        {
            _handlers = new PopupMenuEventHandlers(pRefill, pDataGrid, pCell, pRow);
            _IsReadOnly = pDataGrid.ReadOnly;
        }
        

        internal List<PopupMenuEntry> GetLayout(Cell pCell)
        {
            var _menu = new List<PopupMenuEntry>();
            var _qualifier = new Qualifier();

            if (!_IsReadOnly)
            {
                PopupMenuEntry pme_PopularShifts;
                pme_PopularShifts = new PopupMenuEntry(
                    new PopupMenuEventHandlers.PopulatorDelegate(_handlers.popUp_populatePopularShifts),
                    new PopupMenuEntry.QualifierDelegate(_qualifier.QEditable),
                    false);

                _menu.Add(pme_PopularShifts);
            }

            #region sick / not sick
            ToolStripMenuItem tsmi_Sick;
            PopupMenuEntry pme_Sick;
            tsmi_Sick = new ToolStripMenuItem(Resources.POPUP_Sick,
                null,
                new EventHandler(_handlers.popUp_Sick));
            pme_Sick = new PopupMenuEntry(tsmi_Sick,
                new PopupMenuEntry.QualifierDelegate(_qualifier.QRotaAndEditable),
                true);
            _menu.Add(pme_Sick);

            ToolStripMenuItem tsmi_UnSick;
            PopupMenuEntry pme_UnSick;
            tsmi_UnSick = new ToolStripMenuItem(Resources.POPUP_Unsick,
                null,
                new EventHandler(_handlers.popUp_UnSick));
            pme_UnSick = new PopupMenuEntry(tsmi_UnSick,
                new PopupMenuEntry.QualifierDelegate(_qualifier.QSick),
                true);
            _menu.Add(pme_UnSick);
            #endregion


            ToolStripMenuItem tsmi_ReassignShift;
            PopupMenuEntry pme_ReassignShift;
            tsmi_ReassignShift = new ToolStripMenuItem(Resources.POPUP_AssignShiftTo, null);            
            pme_ReassignShift = new PopupMenuEntry(tsmi_ReassignShift,
                new PopupMenuEventHandlers.PopulatorDelegate(_handlers.popUp_GetOtherEmployees),
                new PopupMenuEntry.QualifierDelegate(_qualifier.QRotaAndShift),
                true);
            pme_ReassignShift.item.Enabled = !_IsReadOnly;
            _menu.Add(pme_ReassignShift);

            ToolStripMenuItem tsmi_DeleteShift;
            PopupMenuEntry pme_DeleteShift;
            tsmi_DeleteShift = new ToolStripMenuItem(Resources.POPUP_Delete_Shift,
                null,
                new EventHandler(_handlers.popUp_DeleteShift));
            pme_DeleteShift = new PopupMenuEntry(tsmi_DeleteShift,
                new PopupMenuEntry.QualifierDelegate(_qualifier.QShift),
                false);
            pme_DeleteShift.item.Enabled = !_IsReadOnly;
            _menu.Add(pme_DeleteShift);

            ToolStripMenuItem tsmi_DeleteAllShifts;
            PopupMenuEntry pme_DeleteAllShifts;
            tsmi_DeleteAllShifts = new ToolStripMenuItem(Resources.POPUP_DeleteAllShifts,
                null,
                new EventHandler(_handlers.popUp_ClearRow));
            pme_DeleteAllShifts = new PopupMenuEntry(tsmi_DeleteAllShifts,
                new PopupMenuEntry.QualifierDelegate(_qualifier.QAnyShiftsInRow),
                true);
            pme_DeleteAllShifts.item.Enabled = !_IsReadOnly;
            _menu.Add(pme_DeleteAllShifts);

            ToolStripMenuItem tsmi_ClearSheet;
            PopupMenuEntry pme_ClearSheet;
            tsmi_ClearSheet = new ToolStripMenuItem("Delete all shifts",
                null,
                new EventHandler(_handlers.popUp_ClearSheet));
            pme_ClearSheet = new PopupMenuEntry(tsmi_ClearSheet,
                new PopupMenuEntry.QualifierDelegate(_qualifier.QAnyShifts),
                true);
            pme_ClearSheet.item.Enabled = !_IsReadOnly;
            _menu.Add(pme_ClearSheet);

            ToolStripMenuItem tsmi_RemoveRow;
            PopupMenuEntry pme_RemoveRow;
            tsmi_RemoveRow = new ToolStripMenuItem(Resources.POPUP_RemoveTemplateRow,
                null,
                new EventHandler(_handlers.popUp_RemoveRow));
            pme_RemoveRow = new PopupMenuEntry(tsmi_RemoveRow,
                new PopupMenuEntry.QualifierDelegate(_qualifier.QTemplate),
                true);
            pme_RemoveRow.item.Enabled = !_IsReadOnly;
            _menu.Add(pme_RemoveRow);
            
            #region Add Notes / Remove Notes
            ToolStripMenuItem tsmi_AddNotes;
            PopupMenuEntry pme_AddNotes;
            tsmi_AddNotes = new ToolStripMenuItem(Resources.POPUP_AddNotes,
                null,
                new EventHandler(_handlers.popUp_AddNotes));
            pme_AddNotes = new PopupMenuEntry(tsmi_AddNotes,
                new PopupMenuEntry.QualifierDelegate(_qualifier.QEditable),
                false);
            pme_AddNotes.item.Enabled = !_IsReadOnly;
            _menu.Add(pme_AddNotes);

            ToolStripMenuItem tsmi_RemoveNotes;
            PopupMenuEntry pme_RemoveNotes;
            tsmi_RemoveNotes = new ToolStripMenuItem(Resources.POPUP_RemoveNotes,
                null,
                new EventHandler(_handlers.popUp_RemoveNotes));
            pme_RemoveNotes = new PopupMenuEntry(tsmi_RemoveNotes,
                new PopupMenuEntry.QualifierDelegate(_qualifier.QEditable),
                false);
            pme_RemoveNotes.item.Enabled = !_IsReadOnly;
            _menu.Add(pme_RemoveNotes);            
            #endregion

            #region Manage holidays / requests
            ToolStripMenuItem tsmi_ManageHolidays;
            PopupMenuEntry pme_ManageHolidays;
            tsmi_ManageHolidays = new ToolStripMenuItem(Resources.POPUP_ManageHolidays,
                null,
                new EventHandler(_handlers.popUp_ManageHolidays));
            pme_ManageHolidays = new PopupMenuEntry(tsmi_ManageHolidays,
                new PopupMenuEntry.QualifierDelegate(_qualifier.QHoliday),
                true);
            _menu.Add(pme_ManageHolidays);
                
            // manage request
            ToolStripMenuItem tsmi_ManageRequest;
            PopupMenuEntry pme_ManageRequest;
            tsmi_ManageRequest = new ToolStripMenuItem(Resources.POPUP_ManageRequest,
                null,
                new EventHandler(_handlers.popUp_ManageRequest));
            pme_ManageRequest = new PopupMenuEntry(tsmi_ManageRequest,
                new PopupMenuEntry.QualifierDelegate(_qualifier.QRequest),
                true);
            _menu.Add(pme_ManageRequest);
            #endregion

            #region those for single _employee
            ToolStripMenuItem tsmi_SeeHolidays;
            PopupMenuEntry pme_SeeHolidays;
            tsmi_SeeHolidays = new ToolStripMenuItem(Resources.POPUP_SeeHolidays,
                null,
                new EventHandler(_handlers.popUp_SeeHolidays));
            pme_SeeHolidays = new PopupMenuEntry(tsmi_SeeHolidays,
                new PopupMenuEntry.QualifierDelegate(_qualifier.QEmployee),
                false);
            _menu.Add(pme_SeeHolidays);

            ToolStripMenuItem tsmi_SeePhones;
            PopupMenuEntry pme_SeePhones;
            tsmi_SeePhones = new ToolStripMenuItem(Resources.POPUP_SeePhoneNumbers,
                null,
                new EventHandler(_handlers.popUp_GetPhoneNumbers));
            pme_SeePhones = new PopupMenuEntry(tsmi_SeePhones,
                new PopupMenuEntry.QualifierDelegate(_qualifier.QEmployee),
                true);
            _menu.Add(pme_SeePhones);
            #endregion

            return _menu;
        }

        public static ContextMenuStrip Build(List<PopupMenuEntry> elements, Cell pCell)
        {
            if (elements == null ||
                elements.Count == 0)
            {
                return null;
            }

            var strip = new ContextMenuStrip();

            foreach (var element in elements)
            {
                bool qualifies = false;
                bool rangeWasEmpty = false;

                if (element.dQualifies == null) // if there is no delegate
                {
                    qualifies = element.bQualifies; // just apply the boolean Value.
                }
                else
                {
                    qualifies = element.dQualifies(pCell); // otherwise "ask" the delegate method.
                }
                if (!qualifies)
                {
                    continue;
                }
                if (element.dPopulates == null &&
                    element.listPrepopulated == null)
                {
                    strip.Items.Add(element.item);
                }
                else
                {
                    var range = new List<ToolStripMenuItem>();
                    if (element.dPopulates != null)
                    {
                        range = element.dPopulates(pCell);
                    }
                    if (element.listPrepopulated != null)
                    {
                        range = element.listPrepopulated;
                    }

                    if (range == null ||
                        range.Count == 0)
                    {
                        rangeWasEmpty = true;
                    }

                    if (!rangeWasEmpty)
                    {
                        if (element.item == null)
                        {
                            strip.Items.AddRange(element.dPopulates(pCell).ToArray());
                        }
                        else
                        {
                            element.item.DropDownItems.AddRange(range.ToArray());
                            strip.Items.Add(element.item);
                        }
                    }
                }

                if (element.separatorAfter &&
                    ((element.item != null) ||
                    (!rangeWasEmpty)))
                {
                    strip.Items.Add(new ToolStripSeparator());
                }
            }

            strip.RemoveRedundantSeparators();
            return strip;
        }
    }
}
